package Top200;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhangmin
 * @create 2022-02-21 17:27
 */
public class combinationSum2_40 {
    List<List<Integer>> res=new LinkedList<>();
    void backtrack(int[] candidates,int target,int start,LinkedList<Integer> path){
        if (target==0){
            res.add(new LinkedList<>(path));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            if (target-candidates[i]<0) break;
            if (i>start&&candidates[i]==candidates[i-1]){
                continue;
            }
            path.addLast(candidates[i]);
            backtrack(candidates,target-candidates[i],i+1,path);
            path.pollLast();
        }
    }

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtrack(candidates,target,0,new LinkedList<>());
        return res;
    }
}
